#!/bin/sh 
#set -x

cd $1

ADDR=$2
DAYS=365


rm -f ${ADDR}_*.pem
rm -f ${ADDR}.p12

case ${ADDR} in
*:*) ALTNAME="URI:${ADDR}" ;;
*@*) ALTNAME="URI:p2p:${ADDR},URI:peer:$3" ;;
*)   ALTNAME="DNS:${ADDR},URI:sip:${ADDR}" ;;
esac
 

rm -f demoCA/index.txt
touch demoCA/index.txt
rm -f demoCA/newcerts/*

export ALTNAME

openssl genrsa  -out ${ADDR}_key.pem 512
openssl req -new  -config openssl.cnf -reqexts cj_req \
        -sha1 -key ${ADDR}_key.pem \
        -out ${ADDR}.csr -days ${DAYS} <<EOF
US
California
Milpitas
sipimp

${ADDR}



EOF

openssl ca -extensions cj_cert -config openssl.cnf \
    -passin pass:password -policy policy_anything \
    -md sha1 -days ${DAYS} -batch -notext -out ${ADDR}_cert.pem \
    -infiles ${ADDR}.csr

mv ${ADDR}_key.pem  ${ADDR}.key
mv ${ADDR}_cert.pem ${ADDR}.cer 
